<?php
require_once( "../Model/db_Promotion.cls.php");
require_once("GCM.cls.php");
require_once("Restaurant.cls.php");
/*
*
*/
class Promotion
{
	private $GOOGLE_KEY = "AIzaSyDfYlODWJrm2SRdcjsZdBvVwZ3DMoRL-t8";
	private $URL = 'https://android.googleapis.com/gcm/send';
	private $clsProm; // 
	public function __construct()
	{
		$this->clsProm = new db_promotion();
	}
	//
	
	public function selectAll($id = '')
	{
		if(!empty($id))
			return false;
 		return $this->clsProm->selectAll();
	}
	//
	
	public function deletePromotion($id = '')
	{
		if(empty($id))
			return false;
		return $this->clsprom->delete($id);
	}
	public function deleteAll()
	{
		return $this->clsProm->deleteAll();
	}
	//
	
	public function insertPromotion($param = array())
	{
		if(empty($param))
			return false;
		foreach ($param as $key => $value)
		{
			$param[$key] = !empty($param[$key])?$param[$key]:"";
		}
		if($this->clsProm->insert($param))
		{
			$gcm = new GCM();
			$res = new Restaurant();
			$registerID = $gcm->getAllGCM();
			$MessagePush = $param['Information'].', giảm giá '.$param['Discount'].'% từ ngày '.$param['DayStart'].' đến ngày '.$param['DayEnd'].' cho khách hàng đến nhà hàng'; 
			$resobj = $res->selectRestaurant($param['RestaurantID']);
			$param['FullName'] = $resobj['FullName'];
			$mess = array('message'=>$MessagePush,'object'=>$param);
			$RegisterIDPush = array();
			for($i=0;$i< sizeof($registerID);$i++)
			{
				$RegisterIDPush[$i] = $registerID[$i]['gcm_regid'];
			}
			$this->sendNotification($RegisterIDPush, $mess);
			return true;
		}else
			return false;
	}
	//
	
	public function updatePromotion($param = array())
	{
		if(!isset($param['id']))
			return false;
		$ID = $param['id'];
		$result = $this->clsProm->select($ID);
		if(empty($result))
			return false;
		foreach($param as $key => $value)
		{
			if (isset($result[$key]))
			{
				$result[$key] = $value;
			}
		}
		return $this->clsRest->updates($result,$ID);
	}
	
	public function sendNotification($registatoin_ids, $message) {
        // include config
        // Set POST variables
        
        $fields = json_encode(array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        ));
        $headers = array(
            'Authorization: key='.$this->GOOGLE_KEY,
            'Content-Type: application/json'
        );
        // Open connection
        $ch = curl_init();
        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $this->URL);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
        // Close connection
        curl_close($ch);
    }
	
}

?>